﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace DeansWsccProject.Account
{
    public partial class Register : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (!OpenAuth.IsLocalUrl(continueUrl))
            {
                continueUrl = "~/";
            }

            OleDbConnection oleDbConn = getDbConnection();
            oleDbConn.Open();
            OleDbCommand profileCmd = new OleDbCommand("INSERT INTO profile (profileUserName) VALUES (@f1)", oleDbConn);
            profileCmd.CommandType = CommandType.Text;
            profileCmd.Parameters.AddWithValue("@f1", RegisterUser.UserName);
            profileCmd.ExecuteNonQuery();
            oleDbConn.Close();

            Response.Redirect(continueUrl);
        }

        private string[] getDataFromTable(string sqlString) //(string selectWhat, string requiredData, string fromTable, string whereField, string equalsKnownValue)
        {
            OleDbConnection oleDbConn = getDbConnection();
            oleDbConn.Open();
            OleDbCommand cmdGetDataFromWhere = new OleDbCommand(sqlString, oleDbConn); //"SELECT " + selectWhat + " " + requiredData + " FROM " + fromTable + " WHERE " + whereField + " = @f1"
            cmdGetDataFromWhere.CommandType = CommandType.Text;
            //cmdGetDataFromWhere.Parameters.AddWithValue("@f1", equalsKnownValue);
            OleDbDataReader dataReader = cmdGetDataFromWhere.ExecuteReader();
            List<string> dataList = new List<string>();
            while (dataReader.Read())
            {
                string rowValue = Convert.ToString(dataReader.GetValue(0));
                dataList.Add(rowValue);
            }
            dataReader.Close();
            oleDbConn.Close();
            return dataList.ToArray();
        }

        private OleDbConnection getDbConnection()
        {
            string connectionStr = getConnectionString();
            OleDbConnection oleDbConn = new OleDbConnection(ConfigurationManager.ConnectionStrings[connectionStr].ConnectionString);
            return oleDbConn;
        }
        private string getConnectionString()
        {
            return "DeansWsccProjectConnectionString";
        }
    }
}